package com.app.server.impl;

import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.mapper.AdminUserMapper;
import com.app.mapper.OpenidMapper;
import com.app.mapper.UnitsManagerMapper;
import com.app.mapper.UserMapper;
import com.app.model.*;
import com.app.server.AdminUserService;
import com.app.utils.CommonHelp;
import com.app.utils.RandomUnits;
import com.app.utils.StringTool;
import com.app.vo.AdminUserVo;
import com.app.vo.AnimalRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.apache.commons.codec.digest.DigestUtils.md5Hex;


@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUserModel> implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private UnitsManagerMapper unitsManagerMapper;

    @Autowired
    private OpenidMapper openidMapper;


    @Override
    public AdminUserVo getAdminList(Integer page, Integer limit, String keyword) {
        Page<AdminUserModel> page1 = new Page<>(page, limit);
        QueryWrapper<AdminUserModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(keyword)) {
            qw.like("username", keyword);
            qw.or().like("real_name", keyword);
        }

        Page<AdminUserModel> adminUserPage = adminUserMapper.selectPage(page1, qw);
        List<AdminUserModel> adminUserList = adminUserPage.getRecords();
        for (AdminUserModel adminUserModel : adminUserList) {
            adminUserModel.setPassword(null);
            if (adminUserModel.getUnitsId() != null) {
                UnitsManagerModel unitsManager = unitsManagerMapper.selectById(adminUserModel.getUnitsId());
                if (unitsManager != null) {
                    adminUserModel.setUnitsName(unitsManager.getName());
                }
            }
        }
        long count = adminUserPage.getTotal();
        AdminUserVo adminUserVo = new AdminUserVo();
        adminUserVo.setList(adminUserList);
        adminUserVo.setCount((int) count);
        return adminUserVo;
    }

    @Override
    public AdminUserVo getAdminUser(Integer page, Integer limit, Integer roleId) {
        Page<AdminUserModel> page1 = new Page<>(page, limit);
        LambdaQueryWrapper<AdminUserModel> lqw = new LambdaQueryWrapper<>();
        if (roleId != 1) {
            lqw.gt(AdminUserModel::getRoleId, roleId);
        }
        Page<AdminUserModel> adminUserPage = adminUserMapper.selectPage(page1, lqw);
        List<AdminUserModel> adminList = adminUserPage.getRecords();

        long count = adminUserPage.getTotal();
        AdminUserVo vo = new AdminUserVo();
        vo.setList(adminList);
        vo.setCount((int) count);
        return vo;
    }

    //根据账号和密码查管理员用户对象
    @Override
    public AdminUserModel login(String username, String md5Password) {
        LambdaQueryWrapper<AdminUserModel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AdminUserModel::getUsername, username);
        lqw.eq(AdminUserModel::getPassword, md5Password);
        AdminUserModel adminUserModel = adminUserMapper.selectOne(lqw);
        return adminUserModel;
    }


    //添加单位管理时默认添加单位管理账号
    @Override
    public void addUnitsManager(UnitsManagerModel unitsManagerModel) {
        Integer id = unitsManagerModel.getId();
        AdminUserModel adminUserModel = new AdminUserModel();
        adminUserModel.setUnitsId(id);
        String username = RandomUnits.generateByRandom(8);
        adminUserModel.setUsername(username);
        String salt = "suBei_bz";
        String passWord = md5Hex("subei123456" + salt);
        adminUserModel.setPassword(passWord);
        adminUserModel.setRoleName("单位管理员");
        //2是单位管理员
        adminUserModel.setRoleId(2);
        adminUserModel.setRealName(unitsManagerModel.getName());
        adminUserModel.setStatus(1);
        adminUserModel.setCreateTime(CommonHelp.getNowTimestamp());
        adminUserMapper.insert(adminUserModel);
    }

    @Override
    public void setUp(Integer id, Integer phoneStatus, String openId, String phone) {
        AdminUserModel adminUserModel = new AdminUserModel();
        if (phoneStatus == 1 && StrUtil.isNotBlank(openId)) {
            openidModel openidModel = new openidModel();
            openidModel.setOpenid(openId);
            openidModel.setMobile(phone);
            openidMapper.insert(openidModel);
        } else {
            QueryWrapper<openidModel> openidModelQueryWrapper = new QueryWrapper<>();
            openidModelQueryWrapper.eq("openid", openId);
            openidMapper.delete(openidModelQueryWrapper);
        }
        adminUserModel.setId(id);
        adminUserModel.setPhoneStatus(phoneStatus);
        adminUserMapper.updateById(adminUserModel);
    }
}
